﻿using Max.PH6_2009A.LowCode.Domain;
using Max.PH6_2009A.LowCode.DTO;
using Max.PH6_2009A.LowCode.IRespository;
using Max.PH6_2009A.LowCode.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH6_2009A.LowCode.Service
{
    public class MenuService : BaseService<MenuModel>, IMenuService
    {
        IMenuRespository _respository;
        public MenuService(IMenuRespository respository) : base(respository)
        {
            _respository = respository;
        }



        public List<MenuRecursionOutputDto> GetRecursionMenu()
        {
            var d = GetAllMenus();
            var data = GetMenus(d, Guid.Empty);
            return data;
        }

        /// <summary>
        /// 获取所有的菜单
        /// </summary>
        /// <returns></returns>
        private IEnumerable<MenuModel> GetAllMenus()
        {
           return _respository.Query("select * from tb_menu");
        }

        //对所有菜单数据进行递归格式
        private List<MenuRecursionOutputDto> GetMenus(IEnumerable<MenuModel> d,Guid id)
        {
            var data = d.Where(x => x.Pid == id);

            List<MenuRecursionOutputDto> list = new();

            foreach (var item in data)
            {
                var temp = GetMenus(d,item.Id);
                list.Add(new MenuRecursionOutputDto
                {
                    Icon = item.Icon,
                    Id = item.Id,
                    Name = item.Name,
                    Url = item.Url,
                    Children = temp

                }); ;
            }
            return list;
        }
    }
}
